<template>
  <!-- 病历form -->
  <el-form :model="form" @submit.native.prevent="onSubmit" label-width="140px" ref="form">
    <el-form-item v-for="(item,index) in form.list" :key="index+item.name" :label="item.name+'：'" :prop="'list.' + index + '.value'" :rules="getRulesType(item)">
      <blModule v-model="item.value" :form="item" :doctorId="doctorId" :deptId="deptId" :cureFirstVisitRecord="cureFirstVisitRecord" @validate="onValidate(index)" @lastPeriod="onLastPeriod"></blModule>
    </el-form-item>
    <el-form-item label="">
      <slot name="footer">
        <el-button v-show="false" type="primary" native-type="submit">保存</el-button>
      </slot>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  props: ["dataForm", "doctorId", "deptId", "cureFirstVisitRecord"],
  components: {
    blModule: () => import("./blModule.vue")
  },
  data() {
    return {
      form: {
        list: []
      },
      isLook: false, //是否监听数据
      rulesBlur: {
        required: true,
        message: "此项必填",
        trigger: "blur"
      },
      rulesChange: {
        required: true,
        message: "此项必填",
        trigger: "change"
      },
      rulesArray: {
        type: "array",
        min: 1,
        message: "此项必填",
        trigger: "change"
      }
    };
  },
  created() {
    let th = this;
    // th.form.curePatientHistory.cureId = th.cureId;
  },
  methods: {
    onSubmit() {
      let th = this;
      th.$refs["form"].validate(v => {
        if (v) th.$emit("submit", th.form.list);
      });
    },
    //返回验证方式
    getRulesType(obj) {
      let result = null;
      if (!obj.notNull) return result;
      switch (obj.component) {
        case "TEXT":
          result = [this.rulesBlur];
          break;
        case "TEXTAREA":
          result = [this.rulesBlur];
          break;
        case "CHECKBOX":
          result = [this.rulesChange];
          break;
        default:
          result = [this.rulesChange];
          break;
      }
      return result;
    },
    onValidate(index) {
      let th = this;
      th.$nextTick(function() {
        th.$refs["form"].validateField(`list.${index}.value`);
      });
    },
    //末次月经计算预产期
    onLastPeriod(value) {
      const th = this; 
      let str = value.replace(/-/g, "/");
      let date = new Date(str);
      date.setDate(date.getDate() + 280);
      let ycq = th.$utils.FormatTime("yyyy-MM-dd", date.getTime());
     
      //循环读取预产期字段
      let data = th.form.list.find(d => {
        return d.name == "预产期";
      }); 
      if (data) {
        data.value = ycq;
      }
    }
  },
  watch: {
    dataForm: {
      deep: true,
      immediate: true,
      handler: function(n) {
        const th = this;
        th.isLook = false;
        th.form.list = [];
        if (n && n.length > 0) {
          th.$nextTick(function() {
            let arr = JSON.parse(JSON.stringify(n));
            th.form.list = arr;
            th.$nextTick(function() {
              th.isLook = true;
            });
          });
        }
      }
    },
    "form.list": {
      deep: true,
      handler: function(n) {
        const th = this;
        if (!th.isLook) return;
        th.$emit("change");
      }
    }
  }
};
</script>